<?php
/**
 * @property Customer $Customer Description
 */
class ElvisController extends AppController {

    public $components = array('Elvis');
    public function beforeFilter() {
        //parent::beforeFilter();
        //echo 1111;die;
        //$this->loadModel("Elvi");
        //echo date("Y-m-d H:i:s",130118000);die;
    }
    public function process() {
        $this->autoRender = false;
        $path = TMP.'S130513.CSV';
        //echo $path;die;
        $data= $this->Elvis->process($path);
        $this->loadModel("Customer");
        $balance_info = array();
        foreach ($data as $value) {
            $money = (int)$value['quantity']*(int)$value['price'];
            $depart_arr = array(310, 391, 393, 410, 430,451, 610, 620, 910, 970, 980, 990);
            $arr = explode(";", $value['item_description']);
            $depart = (int)$arr['4'];
            $point = floor($money);
            if(!in_array($depart, $depart_arr)){
                $point = $point*2;
            }
            $value['point_earned']=$point;
            $this->Elvi->create();
            $this->Elvi->save($value);
            //$arr = array(
            //    'elvi_id'=>  $this->Elvi->id,
            //    'customer_number'=>$value['customer_number'],
            //)
        }
        /*$customer = $this->Customer->find_customer($value['customer_number'],$value['card_number']);
        if (!empty($customer)) {
            $value['is_read']=1;
            $point = ($value['price']*$value['quantity']);
            $this->Customer->id = $customer['Customer']['id'];
            $this->Customer->saveField("point", $point+$customer['Customer']['point']);
            $this->Customer->saveField("balance", $point+$customer['Customer']['balance']);
        }else{
            $this->Elvi->save($value);
        }*/
        echo 'success';
    }
    
    public function transfer_voucher() {
        $this->autoRender=false;
        $this->loadModel("Voucher");
        $date = date("Y-m-d");
        
        $vouchers = $this->Voucher->find('all',array('conditions'=>array('date_format(created, "%Y-%m-%d")'=>$date)));
        
        $file_data = array();
        if(!empty($vouchers)){
            foreach ($vouchers as $voucher) {
                $file_data[]="\"{$voucher['Voucher']['code']}\"|\"U\"|\"{$voucher['Voucher']['value']}\"";
            }
        }
        $file = new File(TMP."vouchers.txt");
        foreach ($file_data as $value) {
            $file->write($value."\n");
        }
        $file->close();
    }
}